<?php
error_reporting ( 0 );
$tit = 'Chat Room';
require_once ("../in/tren.php");
echo '<div class="navigation"><b>Chat Room</b></div><div align="right"><div style="border-top: 3px solid #13cfeb;"></div></div></div><div class="main_menu">';
@session_start ();
if ($user_id && $taikhoan ['quyen'] == 0) {
	echo 'Tài khoản bạn đã bị cấm tham gia diễn đàn!</div>';
} else {
	if ($set ['chat'] != op && $taikhoan ['quyen'] != 2) {
		echo '<b style="color:red">Phòng chat tạm thời đóng cửa!</b></div>';
	} else {
		switch ($_GET ['chat']) {
			default :
				header ( 'location:index.php' );
				break;
			case 'viet' :
				if ($user_id) {
					$login = $taikhoan ['nick'];
				} else {
					$login = $dt;
				}
				$msg = htmlspecialchars ( $_POST ['noidung'] );
				$spam = mysql_fetch_array ( mysql_query ( "select * from spam where ten='" . $login . "' or ip ='" . $ip . "'" ) );
				if (empty ( $_POST ['noidung'] )) {
					echo '<img src="../img/loi.png">Bạn chưa nhập nội dung.<br><a href="index.php">Quay lại</a></div>';
				} elseif (time () - 15 <= $spam ['time']) {
					echo '<img src="../img/loi.png"> Hệ thống ghi nhận bạn vừa mới gửi một bài viết. Hãy chờ ' . (15 - (time () - $spam ['time'])) . ' giây nữa nhé.!<br><a href="index.php">Quay lại</a></div>';
				} elseif (strlen ( $_POST ['noidung'] ) > 500) {
					echo '<img src="../img/loi.png">Nội dung chỉ được phép 500 kí tự. Bạn có tới ' . strlen ( $_POST ['noidung'] ) . ' kí tự!<br><a href="index.php">Quay lại</a></div>';
				} elseif (strlen ( $_POST ['noidung'] ) < 5) {
					echo '<img src="../img/loi.png">Nội dung phải trên 5 kí tự.<br><a href="index.php">Quay lại</a></div>';
				} else {
					mysql_query ( "INSERT INTO `spam` SET `time`='" . time () . "', `ten`='" . $login . "', `ip`='" . $ip . "'" );
					mysql_query ( "INSERT INTO `chat` SET `noidung`='" . $msg . "', `ten`='" . $login . "', `ip`='" . $ip . "', `thoigian`='" . date ( "H:i" ) . "'" );
					require_once ("bot.php");
					if ($bot) {
						mysql_query ( "INSERT INTO `chat` SET `noidung`='" . $bot . "', `ten`='BOT', `ip`='01.02.03.04', `thoigian`='" . date ( "H:i" ) . "'" );
					}
					header ( 'Location:index.php' );
				}
				break;
			case 'q' :
				$array = @mysql_fetch_array ( mysql_query ( "select * from chat where id='{$_GET['id']}'" ) );
				$noidung = $array ['noidung'];
				$ten = $array ['ten'];
				$noidung = preg_replace ( '#\[c=(.+?)\](.+?)\[/c\]#is', '', $noidung );
				$noidung = str_replace ( '
', ' ', $noidung );
				if (strlen ( $noidung ) < 100) {
					$nd = $noidung;
				} else {
					$n = antibb ( substr ( $noidung, 0, 100 ) );
					$nd = '' . $n . '...';
				}
				echo 'Trích dẫn cùng bài viết của bạn tối đa 500kí tự và trên 5kí tự.<br><form action="chat.php?chat=viet" method="post"><textarea name="noidung" cols="20" rows="2">[c=' . $ten . ']' . $nd . '[/c]
</textarea><input type="submit" value="Gửi"></form></div>';
				break;
			case 'sua' :
				if ($taikhoan ['quyen'] != 2) {
					header ( 'location:index.php' );
				} else {
					$id = $_GET ['id'];
					$q = mysql_query ( "SELECT * FROM `chat` WHERE `id`='" . $id . "'" );
					$chat = mysql_fetch_array ( $q );
					echo 'ID Bài Viết:<b>' . $chat ['id'] . '</b><br/><form action="chat.php?chat=luusua&id=' . $chat ['id'] . '" method="post">Thay đổi: <br/><textarea cols="20" rows="2" name="noidung">' . $chat ['noidung'] . '</textarea><br/><input type="submit" value="Thay đổi"></form></div>';
				}
				break;
			case 'luusua' :
				if ($taikhoan ['quyen'] != 2) {
					header ( 'location:index.php' );
				} else {
					$noidung = htmlspecialchars ( $_POST ['noidung'] );
					$q = mysql_query ( "UPDATE `chat` SET `noidung`='" . $noidung . "' WHERE `id`='" . $_GET ["id"] . "'" );
					echo 'Thay đổi thành công!<br/>&raquo;<a href="index.php">Tiếp tục</a></div>';
				}
				break;
			case 'xoa' :
				if ($taikhoan ['quyen'] != 2) {
					header ( 'location:index.php' );
				} else {
					mysql_query ( "DELETE FROM `chat` WHERE `id`='" . ( int ) $_GET ["id"] . "' LIMIT 1;" );
					header ( 'Location:index.php' );
				}
				break;
			case 'xoahet' :
				if ($taikhoan ['quyen'] != 2) {
					header ( 'location:index.php' );
				} else {
					mysql_query ( "TRUNCATE TABLE `chat`;" );
					$ten = $taikhoan ['nick'];
					$botvip = array (1 => "BOT xóa phòng chat rì đó.!!", 2 => "$ten xấu zai đã dọn dẹp phòng chat!", 3 => "$ten xóa phòng chat rì ae.", 4 => "Eo ưi $ten xóa phòng chát rì." );
					srand ( ( double ) microtime () * 100000 );
					$randnum = rand ( 1, 4 );
					$bot = '' . $botvip [$randnum] . '';
					mysql_query ( "INSERT INTO `chat` SET `noidung`='" . $bot . "', `ten`='BOT', `ip`='01.02.03.04', `thoigian`='" . date ( "H:i" ) . "'" );
					header ( 'Location:index.php' );
				}
				break;
		}
	}
}
require_once ("../in/duoi.php");
?>